package com.ellecity06.common.utils

import com.alibaba.sdk.android.oss.ClientConfiguration
import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.OSS
import com.alibaba.sdk.android.oss.OSSClient
import com.alibaba.sdk.android.oss.ServiceException
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult


/**
 * @author ellecity06
 * @time 2024/10/28 18:58
 * @des
 */
class ImageUploadUtils {
    companion object {
        fun upload(
                host: String,
                bucketName: String,
                accessKeyId: String,
                secretKeyId: String,
                securityToken: String,
                path: String,
                fileName:String?=null,
                onFail: (() -> Unit)? = null,
                onResult: ((String) -> Unit)?
        ) {

//            val ossAuthCredentialsProvider = OSSAuthCredentialsProvider(authServerUrl)
            val ossAuthCredentialsProvider = OSSStsTokenCredentialProvider(accessKeyId, secretKeyId, securityToken)
            val conf = ClientConfiguration()
            conf.connectionTimeout = 15 * 1000 // 连接超时，默认15秒

            conf.socketTimeout = 15 * 1000 // socket超时，默认15秒

            conf.maxConcurrentRequest = 5 // 最大并发请求书，默认5个

            conf.maxErrorRetry = 2 // 失败后最大重试次数，默认2次

            val ossClient: OSS = OSSClient(appContext, host, ossAuthCredentialsProvider, conf)

//            logD("图片后缀=$fileTypeName")
            val imageName = try {
                fileName ?: path.substring(path.lastIndexOf("/") + 1)
            } catch (e: Exception) {
                "JDG${System.currentTimeMillis()}"
            }
            val putObjectRequest = PutObjectRequest(bucketName, imageName, path)
            ossClient.asyncPutObject(putObjectRequest,
                    object : OSSCompletedCallback<PutObjectRequest, PutObjectResult> {
                        override fun onSuccess(request: PutObjectRequest?, result: PutObjectResult?) {
                            val imageUrl = ossClient.presignPublicObjectURL(bucketName, imageName)
                            if (result != null) {
                                logD(result.toString())

                            }
                            logD("上传地址=$imageUrl")
                            onResult?.invoke(imageUrl)
                        }

                        override fun onFailure(
                                request: PutObjectRequest?,
                                clientException: ClientException?,
                                serviceException: ServiceException?
                        ) {
                            onFail?.invoke()
                            logD("错误0=${request.toString()}")
                            logD("错误=${clientException}", tr = clientException)
                            logD("错误2", tr = serviceException)
                        }
                    })


        }
    }


}